"use strict";
;(function(Global){
    var $_Ajax = function(Obj){
        Object.assign(this.config,Obj);
        try {
            this.XHR = new XMLHttpRequest();
        } catch (e) {
            this.XHR = new ActiveXObject("Microsoft.XMLHTTP");
        };
        this.init();
    };

    $_Ajax.prototype.config = {
        "url":"",
        "type":"get",
        "data":"",
        "asyns":true,
        "success":function(data){

        },
        "error":function(e){

        }
    };

    $_Ajax.prototype.init = function(){
        this.open();
    };

    $_Ajax.prototype.open = function(){
        var type = this.config.type;
        if(this.config.url==""){
            throw new Error("URL default is empty please pass parameters.");
            return;
        }else if(this.config.data!==""){
			var dataType = typeof this.config.data;
			var datas = "";
			if(dataType === "string"||dataType==="String"){
				this.config.data = this.config.data+"&"+new Date().getTime();
			}else if(dataType=="object"||dataType=="Object"){
				for(var data in this.config.data){
					datas += data+"="+this.config.data[data]+"&"
				}
				this.config.data = datas+new Date().getTime();
			};
		};
        if(type=="get"||type=="GET"){
            this.XHR.open(type,this.config.url+"?"+this.config.data,this.config.asyns);
            this.requestEv();
            this.send();
        } else if(type=="post"||type=="POST"){
            this.XHR.open(type,this.config.url,this.config.asyns);
            this.requestEv();
            this.send(this.config.data);
        }
    };

    $_Ajax.prototype.send=function(data){
        if(data){
            this.XHR.setRequestHeader('Content-type',"application/x-www-form-urlencoded");
            this.XHR.send(data);
        }else{
            this.XHR.send();
        };
    };

    $_Ajax.prototype.requestEv = function(){
		var _this = this;
        this.XHR.onreadystatechange=function(){
            if(this.readyState ==4&&this.status==200){
                var data = this.responseText;
                _this.config.success(data);
            }else{
                _this.config.error(this);
            }
        };
    };

    Object.assign(Global,{
        $_Ajax:function(Obj){
            new $_Ajax(Obj);
        }
    });
    
})(window!=="undefined"?window:this);
